package com.atguigu.order.listener;

import com.atguigu.common.to.mq.SeckillOrderTo;
import com.atguigu.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Component
@RabbitListener(queues = "order.seckill.order.queue")
public class SeckillOrderListener {
  @Resource
  private OrderService orderService;

  @RabbitHandler
  public void createOrder(SeckillOrderTo orderTo, Message message, Channel channel) throws IOException {
    System.out.println("***********接收到秒杀消息");
    long deliveryTag = message.getMessageProperties().getDeliveryTag();
    try {
      orderService.createSeckillOrder(orderTo);
      channel.basicAck(deliveryTag, false);
    } catch (Exception e) {
      channel.basicReject(deliveryTag, true);
    }
  }
}
